package com.whfc.gas.util;

import com.whfc.common.util.ByteUtil;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.MathUtil;
import com.whfc.gas.entity.GasEnum;
import com.whfc.gas.entity.GasMsg;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-08-26 14:42
 */
public class GasMsgUtil {

    /**
     * 帧头
     */
    private static final int FRAME_HEADER = 0x7E;

    /**
     * 帧类型
     */
    private static final int FRAME_TYPE = 0x90;

    /**
     * 解码
     *
     * @param buf
     * @return
     */
    public static GasMsg decode(ByteBuf buf) {

        //帧头
        int frameHeader = buf.readUnsignedByte();

        //长度
        int length = buf.readUnsignedShort();

        //帧类型
        int frameType = buf.readUnsignedByte();

        //mac
        int[] macArr = new int[8];
        for (int i = 0; i < macArr.length; i++) {
            macArr[i] = buf.readUnsignedByte();
        }
        String mac = ByteUtil.toHexString(macArr);

        //短地址
        int shortAddress = buf.readUnsignedShort();

        //自动应答
        int autoReply = buf.readUnsignedByte();

        int dataHeader = buf.readUnsignedByte();
        int dataLength = buf.readUnsignedByte();
        int seq = buf.readUnsignedByte();
        int cmd = buf.readUnsignedByte();
        int attr = buf.readUnsignedByte();
        int version = buf.readUnsignedByte();

        //状态
        int state = buf.readUnsignedShort();

        //时间
        int year = Integer.parseInt(ByteUtil.toHexString(buf.readUnsignedByte())) + 2000;
        int month = Integer.parseInt(ByteUtil.toHexString(buf.readUnsignedByte()));
        int day = Integer.parseInt(ByteUtil.toHexString(buf.readUnsignedByte()));
        int hour = Integer.parseInt(ByteUtil.toHexString(buf.readUnsignedByte()));
        int minute = Integer.parseInt(ByteUtil.toHexString(buf.readUnsignedByte()));
        int second = Integer.parseInt(ByteUtil.toHexString(buf.readUnsignedByte()));
        String timeStr = String.format("%4s-%2s-%2s %2s:%2s:%2s", year, month, day, hour, minute, second);
        Date time = DateUtil.parseDateTime(timeStr);

        //传感器数量
        int sensorNum = buf.readUnsignedByte();

        //传感器值
        Map<GasEnum, Float> valueMap = new HashMap<>(sensorNum);
        for (int i = 0; i < sensorNum; i++) {
            int sensorInfo = buf.readShortLE();
            float sensorValue = buf.readFloatLE();
            int sensorId = sensorInfo & 0x0FFF;
            int unitId = sensorInfo >> 12;
            GasEnum gasEnum = GasEnum.parseByValue(sensorId);
            if (gasEnum != null) {
                valueMap.put(gasEnum, sensorValue);
            }
        }
        //数据段校验和
        int dataCheckSum = buf.readUnsignedByte();

        //校验和
        int checkSum = buf.readUnsignedByte();

        GasMsg msg = new GasMsg();
        msg.setFrameHeader(frameHeader);
        msg.setLength(length);
        msg.setFrameType(frameType);
        msg.setMac(mac);
        msg.setShortAddress(shortAddress);
        msg.setAutoReply(autoReply);
        msg.setDataHeader(dataHeader);
        msg.setDataLength(dataLength);
        msg.setSeq(seq);
        msg.setCmd(cmd);
        msg.setAttr(attr);
        msg.setVersion(String.valueOf(version));
        msg.setState(state);
        msg.setTime(time);
        msg.setSensorNum(sensorNum);
        if (valueMap.containsKey(GasEnum.CO)) {
            msg.setCo(MathUtil.round(valueMap.get(GasEnum.CO), 2));
        }
        if (valueMap.containsKey(GasEnum.O2)) {
            msg.setO2(MathUtil.round(valueMap.get(GasEnum.O2), 2));
        }
        if (valueMap.containsKey(GasEnum.H2S)) {
            msg.setH2s(MathUtil.round(valueMap.get(GasEnum.H2S), 2));
        }
        if (valueMap.containsKey(GasEnum.Ex)) {
            msg.setEx(MathUtil.round(valueMap.get(GasEnum.Ex), 2));
        }
        if (valueMap.containsKey(GasEnum.CH4)) {
            msg.setCh4(MathUtil.round(valueMap.get(GasEnum.CH4), 2));
        }
        if (valueMap.containsKey(GasEnum.EnvRh)) {
            msg.setEnvRh(MathUtil.round(valueMap.get(GasEnum.EnvRh), 2));
        }
        if (valueMap.containsKey(GasEnum.EnvTemp)) {
            msg.setEnvTemp(MathUtil.round(valueMap.get(GasEnum.EnvTemp), 2));
        }
        msg.setCheckSum(checkSum);
        return msg;
    }

    /**
     * 编码
     *
     * @param msg
     * @param buf
     */
    public static void encode(GasMsg msg, ByteBuf buf) {

    }

    public static void main(String[] args) {
        float x = 3.17F;
        ByteBuf buf = Unpooled.buffer();
        buf.writeFloat(x);
        System.out.println(ByteBufUtil.prettyHexDump(buf));
    }
}
